Skip to main content

UV and UVX Cheat Sheet - 2

uv Cheat Sheet

Overview

uv is a tool for managing Python environments and packages. It helps in creating isolated environments and installing packages efficiently.

Installation

To install uv, you can use pip:

pip install uv

Basic Commands

Create a New Environment

uv create myenv

This command creates a new environment named myenv.

Activate an Environment

uv activate myenv

This command activates the environment named myenv.

Deactivate an Environment

uv deactivate

This command deactivates the currently active environment.

Install Packages

uv install package_name

This command installs package_name in the currently active environment.

List Installed Packages

uv list

This command lists all the packages installed in the currently active environment.

Remove Packages

uv remove package_name

This command removes package_name from the currently active environment.

Upgrade Packages

uv upgrade package_name

This command upgrades package_name to the latest version in the currently active environment.

Show Environment Information

uv info

This command displays information about the currently active environment.

Remove an Environment

uv remove myenv

This command removes the environment named myenv.

Advanced Commands

Create an Environment with Specific Python Version

uv create myenv --python 3.8

This command creates a new environment named myenv with Python 3.8.

Install Packages from a Requirements File

uv install -r requirements.txt

This command installs packages listed in requirements.txt in the currently active environment.

Export Installed Packages to a Requirements File

uv export > requirements.txt

This command exports the installed packages in the currently active environment to requirements.txt.

uvx Cheat Sheet

Overview

uvx is a tool for executing commands in a specific Python environment. It helps in running scripts and commands in isolated environments.

Installation

To install uvx, you can use pip:

pip install uvx

Basic Commands

Execute a Command in an Environment

uvx myenv -- python script.py

This command executes script.py in the environment named myenv.

Execute a Command in the Current Environment

uvx -- python script.py

This command executes script.py in the currently active environment.

Run an Interactive Shell in an Environment

uvx myenv -- python

This command starts an interactive Python shell in the environment named myenv.

Run a Command with Specific Python Version

uvx --python 3.8 -- python script.py

This command executes script.py using Python 3.8 in the currently active environment.

Advanced Commands

Execute a Command in a New Environment

uvx newenv --python 3.8 -- python script.py

This command creates a new environment with Python 3.8 and executes script.py in it.

Execute a Command with Environment Variables

uvx myenv -- env VAR=value python script.py

This command executes script.py in the environment named myenv with the environment variable VAR set to value.

Execute a Command with Specific Package Versions

uvx myenv -- pip install package_name==1.0.0 -- python script.py

This command installs package_name version 1.0.0 in the environment named myenv and then executes script.py.

Combining uv and uvx

Create and Execute in a New Environment

uv create myenv --python 3.8 && uvx myenv -- python script.py

This command creates a new environment named myenv with Python 3.8 and then executes script.py in it.

Install Packages and Execute

uv install package_name && uvx -- python script.py

This command installs package_name in the currently active environment and then executes script.py.

This cheat sheet should help you get started with uv and uvx and manage your Python environments and packages efficiently.